import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class TextAreaFormField extends StatefulWidget {
  final int? maxLength;
  final String? initialValue;
  final ValueChanged<String>? onChanged;
  final String? hintText;

  const TextAreaFormField({super.key, this.maxLength, this.initialValue, this.onChanged, this.hintText });

  @override
  State<StatefulWidget> createState() => _TextAreaFormFieldState();

}

class _TextAreaFormFieldState extends State<TextAreaFormField> {
  late String _initialValue;

  @override
  void initState() {
    setState(() {
      _initialValue = widget.initialValue ?? "";
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 6, horizontal: ThemeVars.paddingPage),
      decoration: BoxDecoration(
        border: Border.all(color: ThemeVars.colorBorder),
        borderRadius: const BorderRadius.all(Radius.circular(8)),
      ),
      child: Column(
        children: [
          SizedBox(
            height: 140,
            child: TextFormField(
              minLines: 1,
              maxLines: 8,
              maxLength: widget.maxLength,
              cursorHeight: 16,
              style: TextStyle(fontSize: 14),
              initialValue: _initialValue,
              decoration: InputDecoration(
                hintText: widget.hintText ?? "请输入备注",
                hintStyle: TextStyle(fontSize: 14),
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(vertical: 0),
                isDense: true,
                counterText: "",
              ),
              onChanged: (value) => setState(() {
                _initialValue = value;
                if (widget.onChanged != null) widget.onChanged!(value);
              }),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text("${_initialValue.length}/${widget.maxLength}", style: const TextStyle(fontSize: 12, color: ThemeVars.colorTextSec), textAlign: TextAlign.right,)
            ],
          )
        ],
      ),
    );
  }
}